<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '商品添加' : '商品修改'"
      :visible.sync="info.isshow"
      @close="close"
      @opened="opened"
    >
      <el-form :model="user">
        <el-form-item label="一级分类" label-width="120px">
          <el-select v-model="user.first_cateid" @change="change_first_cate">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" label-width="120px">
          <el-select v-model="user.second_cateid">
            <el-option
              v-for="item in second_cate_list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" label-width="120px">
          <el-input v-model="user.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="价格" label-width="120px">
          <el-input v-model="user.price"></el-input>
        </el-form-item>
        <el-form-item label="市场价格" label-width="120px">
          <el-input v-model="user.market_price"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="120px">
          <upload-img @sendFile="user.img = $event" ref="upload"></upload-img>
        </el-form-item>
        <el-form-item label="商品规格" label-width="120px">
          <el-select v-model="user.specsid" @change="change_specs_id">
            <el-option
              v-for="item in specsList"
              :key="item.id"
              :label="item.specsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格属性" label-width="120px">
          <el-select v-model="user.specsattr" multiple>
            <el-option v-for="item in attr_arr" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否新品" label-width="120px">
          <el-radio v-model="user.isnew" :label="1">是</el-radio>
          <el-radio v-model="user.isnew" :label="2">否</el-radio>
        </el-form-item>
        <el-form-item label="是否热卖" label-width="120px">
          <el-radio v-model="user.ishot" :label="1">是</el-radio>
          <el-radio v-model="user.ishot" :label="2">否</el-radio>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
        <el-form-item label="描述" label-width="120px">
          <div id="editor" v-if="info.isshow"></div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cal">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqcatelist,
  reqgoodsinfo,
  reqgoodsedit,
  reqgoodsadd
} from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins";
import E from "wangeditor";
export default {
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      user: {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: null,
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 2,
        ishot: 2,
        status: 1
      },
      //二级分类的列表
      second_cate_list: [],
      //规格属性的列表
      attr_arr: []
    };
  },
  computed: {
    ...mapGetters({
      // 一级分类列表
      cateList: "cate/arr",
      // 商品规格列表
      specsList: "specs/arr"
    })
  },
  mounted() {
    this.reqCateArr();
    this.reqSpecsList(true);
  },
  methods: {
    ...mapActions({
      reqCateArr: "cate/reqArr",
      reqSpecsList: "specs/reqList",
      reqArr: "goods/reqArr"
    }),
    checkdata() {
      return new Promise((resolve, reject) => {
        if (this.user.first_cateid == "") {
          errorAlert("一级分类不能为空");
          return;
        }
        if (this.user.second_cateid == "") {
          errorAlert("二级分类不能为空");
          return;
        }
        if (this.user.goodsname == "") {
          errorAlert("商品名称不能为空");
          return;
        }
        if (this.user.price == "") {
          errorAlert("价格不能为空");
          return;
        }
        if (this.user.market_price == "") {
          errorAlert("市场价格不能为空");
          return;
        }
        if (this.user.img === null) {
          errorAlert("图片不能为空");
          return;
        }
        if (this.user.specsid == "") {
          errorAlert("商品规格不能为空");
          return;
        }
        if (this.user.specsattr == "") {
          errorAlert("规格属性不能为空");
          return;
        }
        resolve();
      });
    },
    add() {
      this.checkdata().then(() => {
        reqgoodsadd({
          ...this.user,
          specsattr: this.user.specsattr.join(","),
          description: this.editor.txt.html()
        }).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.reqArr();
          }
        });
      });
    },
    empty() {
      this.user = {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: null,
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 2,
        ishot: 2,
        status: 1
      };
      //二级分类的列表
      this.second_cate_list = [];
      //规格属性的列表
      this.attr_arr = [];
      this.$refs.upload.setImgUrl("");
    },
    getOne(id) {
      reqgoodsinfo({ id }).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          this.user.id = id;
          this.get_second_cate_list();
          this.$refs.upload.setImgUrl(this.$pre + this.user.img);
          this.user.specsattr = this.user.specsattr.split(",");
          this.get_specs_attrs();
          if (this.editor) {
            this.editor.txt.html(this.user.description);
          }
        }
      });
    },
    update() {
      this.checkdata().then(() => {
        reqgoodsedit({
          ...this.user,
          specsattr: this.user.specsattr.join(","),
          description: this.editor.txt.html()
        }).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            // 如果修改的是本账号的信息，清空用户数据，并跳转至登录
            this.empty();
            this.info.isshow = false;
            this.reqArr();
          }
        });
      });
    },
    get_second_cate_list() {
      reqcatelist({ pid: this.user.first_cateid }).then(res => {
        if (res.data.code === 200) {
          this.second_cate_list = res.data.list;
        }
      });
    },
    change_first_cate() {
      // 清空二级分类选中的值
      this.user.second_cateid = "";
      // 根据一级分类的选择取出其对应的二级分类的值
      this.get_second_cate_list();
    },
    get_specs_attrs() {
      let obj = this.specsList.find(item => item.id == this.user.specsid);
      this.attr_arr = obj ? obj.attrs : [];
    },
    change_specs_id() {
      this.user.specsattr = [];
      this.get_specs_attrs();
    },
    opened() {
      // 创建富文本编辑器
      this.editor = new E("#editor");
      this.editor.create();

      this.editor.txt.html(this.user.description);
    }
  }
};
</script>
<style scoped lang='less'>
@import "../../../less/index.less";
.inputAndBtn {
  display: flex;
  .el-button {
    width: auto;
  }
  .el-input {
    flex: 1;
  }
}
</style>